package com.runen.wnhz.runen.presenter.iPresenter;

import android.text.TextUtils;
import android.util.Log;

import com.runen.wnhz.runen.common.utils.Base64Utils;
import com.runen.wnhz.runen.common.utils.CountDownButtonHelper;
import com.runen.wnhz.runen.common.utils.MsmEntity;
import com.runen.wnhz.runen.common.utils.RetrofitParameterBuilder;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.BaseEntity;
import com.runen.wnhz.runen.data.entity.BaseUnified;
import com.runen.wnhz.runen.data.entity.MineBean;
import com.runen.wnhz.runen.data.entity.PersonalEntity;
import com.runen.wnhz.runen.data.entity.UploadImgEntity;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.presenter.BasePresenter;
import com.runen.wnhz.runen.presenter.Contart.PersonalContart;
import com.runen.wnhz.runen.presenter.model.PersonalModel;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import okhttp3.RequestBody;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * NAME：薛世杰
 * DATE：2018/5/2
 * EMIAL：zenhac@163.com
 * DESCRIBE：
 */


public class IPersonal extends BasePresenter<PersonalModel, PersonalContart.View> {

    CountDownButtonHelper timeHelper;

    @Inject
    public IPersonal(PersonalModel mModel, PersonalContart.View mView) {
        super(mModel, mView);
    }

    //获取用户详细信息
    public void getUserMessage() {

        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        mModel.getUserMessage(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<PersonalEntity>>() {
                    @Override
                    public void call(BaseEntity<PersonalEntity> personalEntityBaseEntity) {
                        switch (personalEntityBaseEntity.getRe()) {
                            case -1:
                                mView.setLogin(personalEntityBaseEntity.getInfo());
                                break;
                            case 0:
                                mView.setErrorMessage(personalEntityBaseEntity.getInfo());
                                break;
                            case 1:
                                mView.getUserMessage(personalEntityBaseEntity.getData());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });

    }

    //获取验证码
    public void getCode(){

        Log.e("----", "requestMSN: ");

        timeHelper = new CountDownButtonHelper(mView.getTextSendOut(), 60, 1, "#03C066", "#cccccc");

        Map<String, String> map = new HashMap<>();
        String base64Mobile = Base64Utils.encode(mView.getPhone());
        map.put("mobile",base64Mobile);
        map.put("type","4");
        mModel.GetCodeApi(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<MsmEntity>() {
                    @Override
                    public void call(MsmEntity mineBean) {
                        switch (mineBean.getRe()) {
                            case 1:
                                timeHelper.start();
                                mView.showErrorMessage(mineBean.getInfo());
                                break;
                            case 0:
                                mView.showErrorMessage(mineBean.getInfo());
                                break;

                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showErrorMessage(throwable.getMessage().toString());
                    }
                });

    }

    //修改用户信息
    public void setUserData() {

        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        if (!TextUtils.isEmpty(mView.getNickName())){
            mView.setErrorMessage("用户昵称不能为空");
            return;
        } else {
            map.put("username", mView.getNickName());
        }
        map.put("sex", mView.getSex());
        map.put("school", mView.getSchool());
        map.put("place", mView.getPlace());
        map.put("head_img", mView.getHedimg());

        mModel.setUserData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseUnified>() {
                    @Override
                    public void call(BaseUnified baseUnified) {
                        switch (baseUnified.getRe()) {
                            case "-1":
                                mView.setLogin(baseUnified.getInfo());
                                break;
                            case "0":
                                mView.setErrorMessage(baseUnified.getInfo());
                                break;
                            case "1":
                                mView.setSuccess(baseUnified.getInfo());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });

    }

    /*修改密码*/
    public void setUpdatePass() {

        Map<String, String> map = new HashMap<>();

        map.put("token", mView.getToken());
        //新密码
        map.put("password", Base64Utils.encode(mView.getNewPassword()));
        //原有密码
        map.put("wornpassword", Base64Utils.encode(mView.getWornPassword()));
        //确认密码
        map.put("repassword", Base64Utils.encode(mView.getRePassword()));

        mModel.setUserPass(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseUnified>() {
                    @Override
                    public void call(BaseUnified baseUnified) {
                        switch (baseUnified.getRe()) {
                            case "-1":
                                mView.setLogin(baseUnified.getInfo());
                                break;
                            case "0":
                                mView.setErrorMessage(baseUnified.getInfo());
                                break;
                            case "1":
                                mView.setSuccess(baseUnified.getInfo());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });


    }

    /*修改交易密码*/
    public void setUpdatePayPass() {

        if (TextUtils.isEmpty(mView.getPhone())){
            mView.setErrorMessage("手机号不能为空");
            return;
        }

        if (TextUtils.isEmpty(mView.getCode())){
            mView.setErrorMessage("验证码不能为空");
            return;
        }

        if (TextUtils.isEmpty(mView.getPassword())){
            mView.setErrorMessage("登录密码不能为空");
            return;
        }

        String base64Mobile = Base64Utils.encode(mView.getPhone());
        String base64Pasd = Base64Utils.encode(mView.getPassword());

        Map<String, String> map = new HashMap<>();

        map.put("mobile", base64Mobile);
        //新密码
        map.put("code", mView.getCode());
        //原有密码
        map.put("password",base64Pasd);
        //确认密码
        map.put("type", "2");

        mModel.forgetPasswordApi(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<UserBean>>() {
                               @Override
                               public void call(BaseEntity<UserBean> userBeanBaseEntity) {
                                   switch (userBeanBaseEntity.getRe()) {
                                       case -1:
                                           mView.setLogin(userBeanBaseEntity.getInfo());
                                           break;
                                       case 0:
                                           mView.setErrorMessage(userBeanBaseEntity.getInfo());
                                           break;
                                       case 1:
                                           mView.setSuccess(userBeanBaseEntity.getInfo());
                                           break;
                                   }
                               }
                           },
                    new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });


    }

    /*修改昵称*/
    public void setUpdateUserName() {

        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        map.put("username", mView.getNickName());

        mModel.setUserData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseUnified>() {
                    @Override
                    public void call(BaseUnified baseUnified) {
                        switch (baseUnified.getRe()) {
                            case "-1":
                                mView.setLogin(baseUnified.getInfo());
                                break;
                            case "0":
                                mView.setErrorMessage(baseUnified.getInfo());
                                break;
                            case "1":
                                mView.setSuccess(baseUnified.getInfo());
//                                ToastUtil.showToast(baseUnified.getInfo());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });


    }


    //退出登陆
    public void setExitLogin() {

        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        mModel.setExitLogin(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseUnified>() {
                    @Override
                    public void call(BaseUnified baseUnified) {
                        switch (baseUnified.getRe()) {
                            case "-1":
                                mView.setLogin(baseUnified.getInfo());
                                break;
                            case "0":
                                mView.setErrorMessage(baseUnified.getInfo());
                                break;
                            case "1":
                                mView.setSuccess(baseUnified.getInfo());
                                break;
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });
    }

   //账户注销
   public void setCancelUser(String token){
       Map<String, String> map = new HashMap<>();
       map.put("token", token);
       mModel.cancelUser(map)
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Action1<BaseUnified>() {
                   @Override
                   public void call(BaseUnified baseUnified) {
                       switch (baseUnified.getRe()){
                           case "-1":
                               mView.setLogin(baseUnified.getInfo());
                               break;
                           case "0":
                               mView.setErrorMessage(baseUnified.getInfo());
                               break;
                           case "1":
                               mView.setSuccess(baseUnified.getInfo());
                               break;
                       }
                   }
               }, new Action1<Throwable>() {
                   @Override
                   public void call(Throwable throwable) {
                       ToastUtil.showToast(throwable.getMessage());
                   }
               });
   }


    /*修改头像*/
    public void setUpdateUserImg() {

        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        map.put("head_img", mView.getHedimg());

        mModel.setUserData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseUnified>() {
                    @Override
                    public void call(BaseUnified baseUnified) {
                        switch (baseUnified.getRe()) {
                            case "-1":
                                mView.setLogin(baseUnified.getInfo());
                                break;
                            case "0":
                                mView.setErrorMessage(baseUnified.getInfo());
                                break;
                            case "1":
                                mView.setSuccess(baseUnified.getInfo());
                                ToastUtil.showToast(baseUnified.getInfo());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });


    }


    //上传头像
    public void setUploadImage() {


        Map<String, RequestBody> params = RetrofitParameterBuilder.newBuilder()
                .addParameter("file", mView.getFile())
                .bulider(mView.getToken());


        mModel.setUploadImg(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<UploadImgEntity>>() {
                    @Override
                    public void call(BaseEntity<UploadImgEntity> uploadImgEntityBaseEntity) {
                        switch (uploadImgEntityBaseEntity.getRe()) {

                            case -1:
                                mView.setLogin(uploadImgEntityBaseEntity.getInfo());
                                break;
                            case 0:
                                mView.setErrorMessage(uploadImgEntityBaseEntity.getInfo());
                                break;
                            case 1:
                                mView.setSuccess(uploadImgEntityBaseEntity.getData());
                                break;
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }
                });

    }
}
